﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Sector7.Medical.STOM.Windows;
using Sector7.Framework.Enums;
using Sector7.Framework.Utility;
using System.ComponentModel;
using Sector7.Framework.Core;
using Sector7.Medical.Biz;
using Sector7.Medical.EntityType;

namespace Sector7.Medical.STOM.Controls
{
    /// <summary>
    /// VendorSummaryControl.xaml 的交互逻辑
    /// </summary>
    public partial class VendorSummaryControl : UserControl
    {
        #region Accessor
        #endregion

        #region Public functions
        #endregion

        #region Private functions
        private void GetData()
        {
            var worker = BackgroundWorkerUtil.GetWorker(this);
            worker.DoWork += new DoWorkEventHandler(Worker_DoWork);
            worker.RunWorkerAsync();

            Senvironments.ProgressWindow.Visibility = Visibility.Visible;
        }

        private void UpdateDataGridSource(List<M7_Vendor> vendorList)
        {
            ClientUtil.LoadItemsSource(dtgMain, vendorList);

            Senvironments.ProgressWindow.Visibility = Visibility.Hidden;
        }

        private void Worker_DoWork(object sender, DoWorkEventArgs e)
        {
            m_vendorList = Agent_INFS.GetVendorList();

            this.Dispatcher.BeginInvoke(new UpdateDataGridSourceDelegate(UpdateDataGridSource), m_vendorList);
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            var addWindow = new AddVendorWindow();
            addWindow.InitializeWindow(CrudType.Create);
            addWindow.AddSuccess += new EventHandler(AddWindow_AddSuccess);

            addWindow.ShowDialog();
        }

        private void btnUpdate_Click(object sender, RoutedEventArgs e)
        {
            var vendor = dtgMain.SelectedItem as M7_Vendor;
            if (vendor != null)
            {
                var modifyWindow = new AddVendorWindow();
                modifyWindow.Vendor = vendor;
                modifyWindow.InitializeWindow(CrudType.Update);
                modifyWindow.AddSuccess += new EventHandler(AddWindow_AddSuccess);

                modifyWindow.ShowDialog();
            }
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            var vendor = dtgMain.SelectedItem as M7_Vendor;
            if (vendor != null)
            {
                MessageBoxResult dr = Alert.ShowDialog(AlertType.Question, "是否确认删除该供应商信息？");

                if (dr == MessageBoxResult.Yes)
                {
                    Agent_INFS.DeleteVendor(vendor.VendorId);

                    m_vendorList.Remove(vendor);
                    dtgMain.Items.Refresh();
                }
            }
        }

        private void AddWindow_AddSuccess(object sender, EventArgs e)
        {
            var addWindow = sender as AddVendorWindow;
            switch (addWindow.Crud)
            {
                case CrudType.Create:
                    var newVendor = addWindow.Vendor;                    
                    m_vendorList.Add(newVendor);
                    break;
                case CrudType.Update:
                    break;
                default:
                    break;
            }

            dtgMain.Items.Refresh();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            GetData();
        }

        private void searchBox_KeywordsChanging(object sender, KeyEventArgs e)
        {
            string keywords = searchBox.Keywords;
            List<M7_Vendor> filterVendorList = m_vendorList.FindAll(delegate(M7_Vendor __vendor)
            {
                return __vendor.VendorName.Contains(keywords);
            });

            ClientUtil.LoadItemsSource(dtgMain, filterVendorList);
        }

        private void searchBox_KeywordsReset(object sender, EventArgs e)
        {
            ClientUtil.LoadItemsSource(dtgMain, m_vendorList);
        }
        #endregion

        #region Structure
        /// <summary>
        /// 创建供应商概要信息窗口对象
        /// </summary>
        public VendorSummaryControl()
        {
            InitializeComponent();
        }
        #endregion

        #region Inner members
        private List<M7_Vendor> m_vendorList = null;

        private delegate void UpdateDataGridSourceDelegate(List<M7_Vendor> vendorList);
        #endregion
    }
}
